package com.zk.lib_service.service

import android.app.Service
import android.content.Intent
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.util.Log
import com.gc.logger.LogUtils
import com.zk.lib_service.ServiceManager

/**
 * 每隔3秒打印一条日志
 */
class MusicPlayerService : Service() {
    private val binder = MusicServiceBinder(this)
    private val mHandler: Handler = Handler(Looper.getMainLooper())
    override fun onCreate() {
        super.onCreate()
        LogUtils.e("aaaaaaaaa", "MusicPlayerService  onCreate ")
        mHandler.post(backgroundRunnable)

    }

    private var backgroundRunnable: Runnable = Runnable {
        LogUtils.e("aaaaaaaaaaa", "MusicPlayerService backgourndRunnalbe is Alive poccess id = ${android.os.Process.myPid()}")
        reExecte()
    }

    private fun reExecte() {
        mHandler.postDelayed(backgroundRunnable,3000)
    }

    override fun onDestroy() {
        LogUtils.e("aaaaaaaaa", "MusicPlayerService  onDestroy ")
//        ServiceManager.startService2(this)
        super.onDestroy()
//        ServiceManager.startForeService2(this)
        mHandler.removeCallbacks(backgroundRunnable)


    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        LogUtils.e("aaaaaaaaa", "MusicPlayerService  onStartCommand ")
//        return super.onStartCommand(intent, flags, startId)
        return START_STICKY
    }

    override fun onBind(intent: Intent?): IBinder? {
        LogUtils.e("aaaaaaaaa", "MusicPlayerService  onBind ")
        return binder
    }

    override fun onUnbind(intent: Intent?): Boolean {
        LogUtils.e("aaaaaaaaa", "MusicPlayerService  onUnbind ")
        return super.onUnbind(intent)
    }
}